package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class BannerSlice extends AbilitySlice {

    private Image back_button, banner_image;
    private Text text_title, banner_text;
    private DirectionalLayout banner_lin;
    private Button banner_dismiss, banner_long;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_banner);
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Buttons")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Buttons")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Buttons", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Buttons", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Buttons", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        banner_text = (Text) findComponentById(ResourceTable.Id_banner_text);
        banner_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_banner_layout);
        banner_dismiss = (Button) findComponentById(ResourceTable.Id_banner_button);
        banner_long = (Button) findComponentById(ResourceTable.Id_banner_long);
        banner_image = (Image) findComponentById(ResourceTable.Id_banner_image);
        banner_image.setImageAndDecodeBounds(ResourceTable.Media_icon);
        text_title.setText("Banner");
        banner_text.setText("Welcome to Carbon Samples! Feel free to dismiss this banner.");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        banner_dismiss.setClickedListener(component -> banner_lin.setVisibility(Component.HIDE));
        banner_long.setClickedListener(component -> {
        });
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
        banner_lin.setVisibility(Component.VISIBLE);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
